/**
 * 
 */
package com.wubo.common.jdbc;

import java.math.BigDecimal;

/**
 * @author WuBo
 * @CreateDate 2012-4-12 下午03:07:09
 */
public class SQLType {
	public final static int NUMBER = 0;
	
	public final static int STRING = 1;
	
	public final static int CHAR = 2;

	public final static int BOOLEAN = 3;

	public final static int DATE = 4;

	public final static int TIMESTAMP = 5;

	public final static int TIME = 6;
	
	public final static int SHORT = 7;

	public final static int INT = 8;

	public final static int LONG = 9;

	public final static int FLOAT = 10;

	public final static int DOUBLE = 11;
	
	public final static int DECIMAL = 12;
	
	/**
	 * 得到列的类型
	 * @param columnTypeName
	 * @param columnDisplaySize
	 * @return
	 */
	public static int getType(String columnTypeName, int columnDisplaySize){
		if( "BIT".equals(columnTypeName) &&  columnDisplaySize == 1){
			return BOOLEAN;
		}else if("CHAR".equals(columnTypeName) &&  columnDisplaySize == 1){
			return CHAR;
		}else if( "TIMESTAMP".equals(columnTypeName) ){
			return TIMESTAMP;
		}else if("DATE".equals(columnTypeName)){
			return DATE;
		}else if("TIME".equals(columnTypeName)){
			return TIME;
		}else if("TINYINT".equals(columnTypeName)){
			return SHORT;
		}else if("SMALLINT".equals(columnTypeName)){
			return SHORT;
		}else if("INTEGER".equals(columnTypeName)){
			return INT;
		}else if("BYGINT".equals(columnTypeName)){
			return LONG;
		}else if("FLOAT".equals(columnTypeName)){
			return FLOAT;
		}else if("DOUBLE".equals(columnTypeName)){
			return DOUBLE;
		}else if("DECIMAL".equals(columnTypeName)){
			return DECIMAL;
		}
		return STRING;
	}
	
	public static int getValueType(Object value){
		Class<?> type = value.getClass();
		if(Number.class.isAssignableFrom(type)){
			return SQLType.NUMBER;
		}else if(java.sql.Time.class.isAssignableFrom(type)){
			return SQLType.TIME;
		}else if(java.sql.Timestamp.class.isAssignableFrom(type)){
			return SQLType.TIMESTAMP;
		}else if(java.util.Date.class.isAssignableFrom(type)){
			return SQLType.DATE;
		}else if(boolean.class.isAssignableFrom(type) ||
				Boolean.class.isAssignableFrom(type)){
			return SQLType.BOOLEAN;
		}else if(char.class.isAssignableFrom(type) ||
				Character.class.isAssignableFrom(type)){
			return SQLType.CHAR;
		}else if(BigDecimal.class.isAssignableFrom(type)){
			return SQLType.DECIMAL;
		}
		return SQLType.STRING;
	}
	
}
